﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SFP;
using SFP.Animation2D;

namespace LevelEditor
{
    public class ObjectLE
    {
        public readonly long id;

        private Panel objectPanel;
        private Label objectNameLabel;
        private PictureBox objectPictureBox;
        private Panel objectsPanel;
        private LevelEditorForm levelEditorForm;
        private Dictionary<GameObject, Layer> linkedGameObjects;

        public ObjectLE(long id, Panel objectPanel, Label objectNameLabel, PictureBox objectPictureBox, LevelEditorForm levelEditorForm)
        {
            this.id = id;
            this.objectPanel = objectPanel;
            this.objectNameLabel = objectNameLabel;
            this.objectPictureBox = objectPictureBox;
            this.objectsPanel = (Panel)objectPanel.Parent;
            this.levelEditorForm = levelEditorForm;

            linkedGameObjects = new Dictionary<GameObject, Layer>();
        }

        private string _name;
        public string Name {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                objectNameLabel.Text = value;
            }
        }
        public string Type { get; set; }
        public Bitmap ObjectImg { get; set; }
        public string ObjectImgFileName { get; set; }
        public string ObjectImgPath { get; set; }
        public bool IsDragDropped { get; set; }

        public Panel GetObjectPanel()
        {
            return objectPanel;
        }

        public Label GetObjectNameLabel()
        {
            return objectNameLabel;
        }

        public PictureBox GetObjectPictureBox()
        {
            return objectPictureBox;
        }

        public Dictionary<GameObject, Layer> GetLinkedGameObjects()
        {
            return new Dictionary<GameObject, Layer>(linkedGameObjects);
        }

        public void AddLinkedGameObject(GameObject gameObject, Layer layer)
        {
            linkedGameObjects.Add(gameObject, layer);
        }

        public void RemoveLinkedGameObject(GameObject gameObject)
        {
            linkedGameObjects.Remove(gameObject);
        }

        public void objectPanel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                levelEditorForm.SetActiveObjectLE(this);
        }

        public void objectPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            new ObjectPreferencesForm(this, levelEditorForm).ShowDialog();
        }

        public void objectPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (ObjectImg == null) return;

            levelEditorForm.SetActiveObjectLE(this);
            IsDragDropped = true;
            objectPictureBox.DoDragDrop(ObjectImg, DragDropEffects.Copy);
            IsDragDropped = false;
        }
    }
}
